﻿using STF.Blog.Dtos;
using STF.Blog.IRepository;
using STF.Blog.IService;
using STF.Blog.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STF.Blog.Service
{
    public class UsersInfoService : IUsersInfoService
    {
        private IUsersInfoManager _uf;

        public UsersInfoService(IUsersInfoManager uf)
        {
            _uf = uf;
        }

        public async Task<int> AddUsersInfoAsync(string realName, DateTime birthday, int gender, string weChat, string tel, string address, string photo, decimal amount, int uid)
        {
            return await _uf.AddAsync(new UsersInfo
            {
                RealName = realName,
                Birthday = birthday,
                Gender = (Common.Gender)gender,
                WeChat = weChat,
                Tel = tel,
                Address = address,
                Photo = photo,
                Amount = amount,
                Uid = uid
            });
        }

        public async Task<int> DeleteUsersInfoAsync(int id)
        {
            var data = await _uf.GetById(id);
            if (data == null)
                return -1;
            if (data.DeleteId != 1)
                return -2;
            return await _uf.DeleteAsync(data);
        }

        public async Task<List<UsersInfoDto>> GetAllUsersInfoAsync(int deleteId = 1)
        {
            var data = await _uf.GetChooseAsync(ufo => ufo.DeleteId == deleteId);
            return data == null ? new List<UsersInfoDto>()
                : data.Select(ufo => new UsersInfoDto 
                {
                    Id = ufo.Id,
                    RealName = ufo.RealName,
                    Birthday = ufo.Birthday,
                    Gender = ufo.Gender,
                    WeChat = ufo.WeChat,
                    Tel = ufo.Tel,
                    Address = ufo.Address,
                    Photo = ufo.Photo,
                    Amount = ufo.Amount,
                    Uid = ufo.Uid
                }).ToList();
        }

        public async Task<UsersInfoDto> GetByIdAsync(int id)
        {
            var data = await _uf.GetById(id);
            return data == null ? new UsersInfoDto() : new UsersInfoDto
            {
                Id = data.Id,
                RealName = data.RealName,
                Birthday = data.Birthday,
                Gender = data.Gender,
                WeChat = data.WeChat,
                Tel = data.Tel,
                Address = data.Address,
                Photo = data.Photo,
                Amount = data.Amount,
                Uid = data.Uid
            };
        }

        public async Task<List<UsersInfoDto>> GetByNameAsync(string realName, int deleteId = 1)
        {
            var data = await _uf.GetChooseAsync(ufo => ufo.DeleteId == deleteId && ufo.RealName.Contains(realName));
            return data == null ? new List<UsersInfoDto>() : data.Select(ufo => new UsersInfoDto
            {
                Id = ufo.Id,
                RealName = ufo.RealName,
                Birthday = ufo.Birthday,
                Gender = ufo.Gender,
                WeChat = ufo.WeChat,
                Tel = ufo.Tel,
                Address = ufo.Address,
                Photo = ufo.Photo,
                Amount = ufo.Amount,
                UpdateTime = ufo.UpdateTime,
                Uid = ufo.Uid
            }).ToList();
        }

        public async Task<UsersInfoDto> GetUserIdAsync(int userId)
        {
            var data = await _uf.GetChooseAsync(u => u.DeleteId == 1 && u.Uid == userId);
            return data == null ? new UsersInfoDto() : new UsersInfoDto 
            {
                Id = data[0].Id,
                RealName = data[0].RealName,
                Birthday = data[0].Birthday,
                Gender = data[0].Gender,
                DeleteId = data[0].DeleteId,
                Address = data[0].Address,
                Tel = data[0].Tel,
                Amount = data[0].Amount,
                Photo = data[0].Photo,
                Uid = data[0].Uid,
                WeChat = data[0].WeChat
            };
        }

        public async Task<int> PutTrashAsync(int id)
        {
            var data = await _uf.GetById(id);
            if (data == null || data.DeleteId == 0)
                return -1;
            data.DeleteId = 0;
            return await _uf.UpdateAsync(data);
        }

        public async Task<int> RestoreAsync(int id)
        {
            var data = await _uf.GetById(id);
            if (data == null || data.DeleteId == 1)
                return -1;
            data.DeleteId = 1;
            return await _uf.UpdateAsync(data);
        }

        public async Task<int> UpdateUsersInfoAsync(int id, string realName, DateTime birthday, int gender, string weChat, string tel, string address, string photo, decimal amount, int uid)
        {
            var data = await _uf.GetById(id);
            if (data == null)
                return -1;
            data.RealName = realName;
            data.Birthday = birthday;
            data.Gender = (Common.Gender)gender;
            data.WeChat = weChat;
            data.Tel = tel;
            data.Address = address;
            data.Photo = photo;
            data.Amount = amount;
            data.Uid = uid;
            return await _uf.UpdateAsync(data);
        }
    }
}
